const mongoose = require('mongoose');
mongoose.Promise = require("bluebird");
const moment = require('moment');
const createRequest = require('./createRequest');
const token = '';
const port = 8888;
const type = 'http';
const host = 'localhost';

const now = () => {
  return moment().format('YYYY-MM-DD HH:mm:ss')
}

Promise.resolve("开始测试")
  // .then(mes => { //建立连接
  //   console.log(mes);
  //   return mongoose.connect('mongodb://localhost:27017/jusexcx')
  // })
  // .then(mes => { //清空数据库
  //   return mongoose.connection.dropDatabase()
  // })
  // .then(mes => { //建立管理人员账户
  //   const Schema = mongoose.Schema;
  //   const serviceloginSchema = new Schema({
  //     userid: { type: String },//id
  //     name: { type: String },//用户名称
  //     loginstate: { type: Number },//登陆状态
  //     date: { type: String },//登陆时间
  //     myassistant: { type: Array }//我的助理
  //   });
  //   const servicelogin = mongoose.model('servicelogin', serviceloginSchema);
  //   return servicelogin.create([
  //     {
  //       userid: '1助理id',//id
  //       name: '1助理',//用户名称
  //       loginstate: 1,//登陆状态
  //       date: now(),//登陆时间
  //       myassistant: []//我的助理
  //     },
  //     {
  //       userid: '1医生id',//id
  //       name: '1医生',//用户名称
  //       loginstate: 1,//登陆状态
  //       date: now(),//登陆时间
  //       myassistant: ['1助理id']//我的助理
  //     },
  //     {
  //       userid: '1药师id',//id
  //       name: '1药师',//用户名称
  //       loginstate: 1,//登陆状态
  //       date: now(),//登陆时间
  //       myassistant: []//我的助理
  //     },
  //   ])
  // })
  // .then(mes => { //关闭连接
  //   return mongoose.connection.close();
  // })
  // .then(mes => { //建立管理人员账户成功
  //   return '建立用户成功'
  // })
  .then(mes => { //建立用户档案
    console.log(mes);
    return createRequest({
      port: port,
      host: host,
      type: type,
      method: 'POST',
      path: '/server/saveUserMes',
      token: token,
      data: {
        userid: '1用户id',//用户id 商城登陆用户id 小程序openid
        name: '1用户',//姓名
        birthday: '2000-11-11',//出生日期
        gender: 1,//性别
        height: 180,//身高
        weight: 65,//体重
        medicalHistory: '无',//病史
        allergy: '无',//过敏史
        //基本情况
        information: JSON.stringify({//基本情况收集单 
          drink: '1天1次'
        }),
        serMedicinal: JSON.stringify([{ 药名: "非处方药1", isdrugstatus: "N" }, { 药名: "非处方药2", isdrugstatus: "N" }]),
        serverId: "1客服id",
        serverName: '1客服',
        assistantId: "1助理id",
        assistantName: "1助理",
      }
    })
  })
  .then(mes => {//建立用户档案
    console.log(mes);
    return createRequest({
      port: port,
      host: host,
      type: type,
      method: 'POST',
      path: '/server/saveUserMes',
      token: token,
      data: {
        userid: '2用户id',//用户id 商城登陆用户id 小程序openid
        name: '2用户',//姓名
        birthday: '2001-11-11',//出生日期
        gender: 1,//性别
        height: 180,//身高
        weight: 65,//体重
        medicalHistory: '无',//病史
        allergy: '无',//过敏史
        //基本情况
        information: JSON.stringify({//基本情况收集单 
          drink: '1天1次'
        }),
        serMedicinal: JSON.stringify([{ 药名: "非处方药1", isdrugstatus: "N" }, { 药名: "非处方药2", isdrugstatus: "N" }]),
        serverId: "1客服id",
        serverName: '1客服',
        doctorId: "1医生id",
        doctorName: "1医生",
      }
    })
  })
  .then(mes => {//建立用户档案
    console.log(mes);
    return createRequest({
      port: port,
      host: host,
      type: type,
      method: 'POST',
      path: '/server/saveUserMes',
      token: token,
      data: {
        userid: '3用户id',//用户id 商城登陆用户id 小程序openid
        name: '3用户',//姓名
        birthday: '2001-11-11',//出生日期
        gender: 1,//性别
        height: 180,//身高
        weight: 65,//体重
        medicalHistory: '无',//病史
        allergy: '无',//过敏史
        //基本情况
        information: JSON.stringify({//基本情况收集单 
          drink: '1天1次'
        }),
        serMedicinal: JSON.stringify([{ 药名: "非处方药1", isdrugstatus: "N" }, { 药名: "非处方药2", isdrugstatus: "N" }]),
        serverId: "2客服id",
        serverName: '2客服',
        assistantId: "1助理id",
        assistantName: "1助理",
      }
    })
  })
  .then(mes => {//查询助理带处理
    console.log(mes);
    return createRequest({
      port: port,
      host: host,
      type: type,
      method: 'POST',
      path: '/server/assDocPending',
      token: token,
      data: {
        id: '1助理id',
        userRole: 3
      }
    })
  })
  .then(mes => {
    console.log(mes);
    mes = JSON.parse(mes);
    return Promise.all([
      createRequest({ //发送给医生
        port: port,
        host: host,
        type: type,
        method: 'POST',
        path: '/server/assSendDoc',
        token: token,
        data: {
          _id: mes.data[0]._id,
          userRole: 3,
          doctorId: '1医生id',
          doctorName: '1医生',
          assMedicinal: JSON.stringify([{ 药名: '处方药1', isdrugstatus: 'Y' }, { 药名: '非处方药1', isdrugstatus: 'N' }]),
        }
      }),
      createRequest({ //助理生成诊断单
        port: port,
        host: host,
        type: type,
        method: 'POST',
        path: '/server/createCertificate',
        token: token,
        data: {
          _id: mes.data[1]._id,
          userRole: 3,
          id: '1助理id',
          name: '1助理',
          medicinal: JSON.stringify([{ 药名: '处方药1', isdrugstatus: 'Y' }, { 药名: '非处方药1', isdrugstatus: 'N' }]),
        }
      })
    ])
  })
  .then(mes => {//查询医生带处理
    console.log(mes);
    return createRequest({
      port: port,
      host: host,
      type: type,
      method: 'POST',
      path: '/server/assDocPending',
      token: token,
      data: {
        id: '1医生id',
        userRole: 1
      }
    })
  })
  .then(mes => {//医生处理
    console.log(mes);
    mes = JSON.parse(mes);
    return Promise.all([
      createRequest({ //医生生成诊断单
        port: port,
        host: host,
        type: type,
        method: 'POST',
        path: '/server/createCertificate',
        token: token,
        data: {
          _id: mes.data[0]._id,
          userRole: 1,
          id: '1医生id',
          name: '1医生',
          medicinal: JSON.stringify([{ 药名: '处方药1', isdrugstatus: 'Y' }, { 药名: '非处方药1', isdrugstatus: 'N' }]),
        }
      }),
      createRequest({ //医生生成诊断单
        port: port,
        host: host,
        type: type,
        method: 'POST',
        path: '/server/createCertificate',
        token: token,
        data: {
          _id: mes.data[1]._id,
          userRole: 1,
          id: '1医生id',
          name: '1医生',
          medicinal: JSON.stringify([{ 药名: '处方药3', isdrugstatus: 'Y' }, { 药名: '非处方药3', isdrugstatus: 'N' }]),
        }
      }),
    ])
  })
  .then(mes => {//查询所有待审核凭证
    console.log(mes);
    return createRequest({
      port: port,
      host: host,
      type: type,
      method: 'POST',
      path: '/server/allCertificate',
      token: token,
      data: {}
    })
  })
  .then(mes => {
    mes = JSON.parse(mes);
    console.log(mes.Rx_Drug.length);
    console.log(mes.OTC.length);
    mes.data = [].concat(mes.Rx_Drug, mes.OTC)
    return Promise.all([
      createRequest({ //药师审核
        port: port,
        host: host,
        type: type,
        method: 'POST',
        path: '/server/pharmacistCheck',
        token: token,
        data: {
          _id: mes.data[0]._id,
          result: 0,
          id: '1药师id',
          name: '1药师',
        }
      }),
      createRequest({ //医生生成诊断单
        port: port,
        host: host,
        type: type,
        method: 'POST',
        path: '/server/pharmacistCheck',
        token: token,
        data: {
          _id: mes.data[1]._id,
          result: 1,
          id: '1药师id',
          name: '1药师',
        }
      }),
      createRequest({ //医生生成诊断单
        port: port,
        host: host,
        type: type,
        method: 'POST',
        path: '/server/pharmacistCheck',
        token: token,
        data: {
          _id: mes.data[2]._id,
          result: 1,
          id: '1药师id',
          name: '1药师',
        }
      }),
    ])
  })
  .then(mes => {
    console.log(mes)
  })